package com.atguigu.gmall.mq.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.mq.config.DeadLetterConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import com.atguigu.gmall.rabbit.service.RabbitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: atguigu
 * @create: 2023-09-16 15:32
 */
@RestController
@RequestMapping("/api/mq")
@Slf4j
public class ProducerController {


    @Autowired
    private RabbitService rabbitService;

    /**
     * 生产者发送消息
     *
     * @param data
     * @return
     */
    @GetMapping("/sendNormalMsg")
    public Result sendNormalMsg(@RequestParam("data") String data) {
        String exchange = "exchange.confirm";
        String routingKey = "routing.confirm";
        rabbitService.sendMessage(exchange, routingKey, data);
        log.info("发送消息成功");
        return Result.ok();
    }

    /**
     * 基于死信实现延迟消息
     *
     * @param data
     * @return
     */
    @GetMapping("/sendDeadLetterMsg")
    public Result sendDeadLetterMsg(@RequestParam("data") String data) {
        rabbitService.sendMessage(DeadLetterConfig.exchange_dead, DeadLetterConfig.routing_dead_1, data);
        log.info("基于死信-发送延迟消息成功");
        return Result.ok();
    }


    /**
     * 基于延迟插件实现延迟消息
     *
     * @param data
     * @return
     */
    @GetMapping("/sendDelayMsg")
    public Result sendDelayMsg(@RequestParam("data") String data, @RequestParam("delayTime") int delayTime) {
        rabbitService.sendDelayMessage(DelayedMqConfig.exchange_delay, DelayedMqConfig.routing_delay, data, delayTime);
        log.info("基于延迟插件-发送延迟消息成功");
        return Result.ok();
    }
}
